package com.shouhuobao.take.wallet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.BankRequestBean;
import com.shouhuobao.take.model.CapitalHistoryBean;
import com.shouhuobao.take.wallet.adapter.TakeMoneyHistoryAdapter;

import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * Created by joker on 2017/8/11.
 * 提现记录页面
 */
public class TakeMoneyHistoryActivity extends BaseActivity2 implements View.OnClickListener {

    private SListView mListView;
    private TakeMoneyHistoryAdapter mAdapter = null;
    private ReqResult<BankRequestBean> result = null;

    private int isFrom = 0;//0 不进行返回刷新 1进行返回刷新

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().queryTakeMoneyHistoryData(1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_wallet_takemoney_history);
        super.findViewById();
        getAppTitle().setCommonTitle("提现记录");
        //设置标题右边显示
        findViewById(getResources().getIdentifier("title_right_switcher", "id", getPackageName())).setVisibility(View.VISIBLE);
        int right = getResources().getIdentifier("title_right_text", "id", getPackageName());
        TextView rightTV = (TextView) findViewById(right);
        rightTV.setText("联系客服");
        rightTV.setTextColor(getResources().getColor(R.color.app_font_green));
        rightTV.setOnClickListener(this);

        mListView = (SListView) findViewById(R.id.listview);
        mAdapter = new TakeMoneyHistoryAdapter(null, getContext());
        mListView.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().queryTakeMoneyHistoryData(1);
            }
        });
        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                if (result != null && result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().queryTakeMoneyHistoryData(result.getPageNum() + 1);
                }
            }
        });

        isFrom = getIntent().getIntExtra("isFrom", 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //联系客服
            case R.id.title_right_text:
                DialogMgr.showPhoneDialog(getContext(), null);
                break;
        }
    }

    @Override
    public void finish() {
        if (isFrom == 1) {
            setResult(Activity.RESULT_OK, new Intent());
        }
        super.finish();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.query_take_money_history_data:
                cancelLoadingDialog();
                onRefreshComplete();
                result = JSON.parseList(msg.obj, BankRequestBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            if (result.getPageNum() == 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });

                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
